package com.zb.admin.controller;

import com.zb.admin.service.AdChannelService;
import com.zb.model.common.PageResult;
import com.zb.model.common.Result;
import com.zb.model.dto.admin.ChannelDTO;
import com.zb.model.pojo.admin.AdChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/channel")
public class AdChannelController {
    @Autowired
    private AdChannelService channelService;

    //分页查频道
    @GetMapping("/list")
    public Result findByNameAndPage(ChannelDTO dto) {
        PageResult pageResult = channelService.pageList(dto);
        return Result.SUCCESS(pageResult);
    }

    //添加频道
    @PostMapping("/save")
    public Result insert(@RequestBody AdChannel channel) {
        Integer num = channelService.insert(channel);
        if(num > 0) {
            return Result.SUCCESS("添加频道成功");
        }else {
            return Result.ERROR();
        }
    }

    //   根据id查询频道
    @GetMapping("/{id}")
    public Result update(@PathVariable Integer id) {
        return Result.SUCCESS(channelService.getById(id));
    }

    //    频道修改
    @PutMapping("/update")
    public Result update(@RequestBody AdChannel adChannel) {
        Integer num =  channelService.update(adChannel);
        if(num > 0) {
            return Result.SUCCESS("更新频道成功");
        }else {
            return Result.ERROR();
        }
    }

//    根据频道ID删除
    @DeleteMapping("/del/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        Integer num = channelService.deleteById(id);
        if (num > 0) {
            return Result.SUCCESS("删除频道成功");
        } else {
            return Result.ERROR();
        }
    }
}
